<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/5
 * Time: 16:17
 */

namespace App\Game;

use Carbon\Carbon;
use App\Game\Traits\FyTrait;

class fy extends Game
{
    use FyTrait;

    private $platName = 'fy';

    const API_REGISTER = '/api/user/register';
    const API_GET_BALANCE = '/api/user/balance';
    const API_TRANSACTION = '/api/user/transfer';
    const API_CHECK_TRANSFER = '/api/user/transferinfo';
    const API_JOIN_GAME = '/api/user/login';
    const API_GUEST_JOIN_GAME = '/api/user/guest';
    const API_BET_RECORD = '/api/log/get';
    const API_GAME_LIST = '/api/log/category';


    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
//            'status' => 1,
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/24 14:16
     * Administrator
     * curlCreateMember
     * 创建用户
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $params = [
            'UserName' => $accountUserName,
            'password' => $password,
        ];
        $url = $this->gameUrl . self::API_REGISTER;

        $output = $this->fyRequest('POST', $url, $params);

        if (array_key_exists('success', $output)) {
            if ($output['success'] == 1) {
                return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：' . $output['msg']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 14:15
     * Administrator
     * curlGetBalance
     * 查询余额
     * @param $accountUserName
     * @return array
     */
    public function curlGetBalance($accountUserName)
    {
        $params = [
            'UserName' => $accountUserName,
        ];
        $url = $this->gameUrl . self::API_GET_BALANCE;

        $output = $this->fyRequest('POST', $url, $params);
        if (array_key_exists('success', $output)) {
            if ($output['success'] == 1 && $output['msg'] == '查询成功') {
                return ['success' => true, 'data' => ['balance' => $output['info']['Money']], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '查询余额失败，错误原因：' . $output['msg']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }


    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $params = [
            'UserName' => $accountUserName,
            'Money' => $price,
            'Type' => 'IN',
            'ID' => $transferid,
        ];
        $url = $this->gameUrl . self::API_TRANSACTION;

        $output = $this->fyRequest('POST', $url, $params);

        if (array_key_exists('success', $output)) {
            if ($output['success'] == 1 && $output['msg'] == '转入成功') {
                return ['success' => true, 'data' => ['transferid' => $output['info']['ID'], 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转入失败，错误原因：' . $output['msg']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $params = [
            'UserName' => $accountUserName,
            'Money' => $price,
            'Type' => 'OUT',
            'ID' => $transferid,
        ];
        $url = $this->gameUrl . self::API_TRANSACTION;

        $output = $this->fyRequest('POST', $url, $params);

        if (array_key_exists('success', $output)) {
            if ($output['success'] == 1 && $output['msg'] == '转出成功') {
                return ['success' => true, 'data' => ['transferid' => $output['info']['ID'], 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转出失败，错误原因：' . $output['msg']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $params = [
            'ID' => $request->transferid,
        ];
        $url = $this->gameUrl . self::API_CHECK_TRANSFER;

        $output = $this->fyRequest('POST', $url, $params);

        if (array_key_exists('success', $output)) {
            if ($output['success'] == 1 && $output['msg'] == '已完成') {
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
            } elseif ($output['success'] == 0) {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '查询转账失败，错误原因：' . $output['msg']];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '查询转账失败，错误原因：' . $output['msg']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //踢线
    public function curlKick($accountUserName)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线功能'];
    }

    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinGame
     * 进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinGame($request)
    {
        if ($request->demo) {
            $url = $this->gameUrl . self::API_GUEST_JOIN_GAME;
            $params = [];
        } else {
            $url = $this->gameUrl . self::API_JOIN_GAME;
            $params = [
                'username' => $request->accountUserName,
            ];
        }

        $output = $this->fyRequest('POST', $url, $params);

        if (array_key_exists('success', $output)) {
            if ($output['success'] == 1 && $output['msg'] == '登录成功') {
                return ['success' => true, 'data' => ['url' => $output['info']['Url']], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['msg']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }


    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinMobileGame
     * 手机端进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        return $this->curlJoinGame($request);
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $totalPage = $page = 1;
        $start_time = $startTime->toDateTimeString();
        $end_time = $endTime->toDateTimeString();

        $params = [
            'Type' => 'UpdateAt',
            'StartAt' => $start_time,
            'EndAt' => $end_time,
            'PageIndex' => $page,
        ];
        $url = $this->gameUrl . self::API_BET_RECORD;

        do {
            $output = $this->fyRequest('POST', $url, $params);

            if (array_key_exists('success', $output)) {
                if ($output['success'] == 1) {
                    $result = $output['info']['list'];
                    $totalPage = $output['info']['RecordCount'] / $output['info']['PageSize'];
                    if (count($result) >= 1) {
                        $data = array_merge($data, $result);
                    }
                }
            }
            $page++;
        } while ($page <= $totalPage);
        //存入游戏列表库
        $this->getGameList($data);

        return $data;
    }
}